package com.sctu.EMSApp;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.sctu.adapter.ViewPagerAdapter;
import com.sctu.bean.PlayFoodOneDetailsInfo;
import com.sctu.http.UrlConfig;
import com.sctu.httpvollry.GetVolleyHttp;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/12/13.
 */
public class PalyFoodOneDetailsActivity extends Activity {

    private TextView headtitleitext;
    private TextView introducetext;
    private TextView phonetext;
    private TextView addresstext;
    private ImageView returnfragment;
    private ImageView imageView;
    private List<View> listviews;
    private List<ImageView> listimageView = new ArrayList<ImageView>();
    private ViewPager viewPager;
    private View view;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.food_details);
        initView();
    }

    private void initView() {
        int id = getIntent().getIntExtra("id", 0);
        //对返回按钮监控，进行返回
        returnfragment = (ImageView) findViewById(R.id.food_details_return_img);
        returnfragment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PalyFoodOneDetailsActivity.this.finish();
            }
        });
        GetVolleyHttp.getInstance(this).getStringGETJsonData(UrlConfig.URL_FOOD_DETAILS_LIST_ONE + id, listener, errorlistener);

    }

    // 当网络请求返回成功（200的状态）
    Response.Listener<String> listener = new Response.Listener<String>() {
        @Override
        public void onResponse(String json) {
            Log.e("Json", json);
            // 开始解析JSON
            // 反序列化，直接将JSON反序列化为Test这个Class对象
            PlayFoodOneDetailsInfo results = JSON.parseObject(json,
                    PlayFoodOneDetailsInfo.class);
            view = LayoutInflater.from(PalyFoodOneDetailsActivity.this).inflate(R.layout.food_details, null);
            //对头部的title进行设置
            headtitleitext = (TextView) findViewById(R.id.food_details_title_txt);

            headtitleitext.setText(results.getResults().get(0).getTitle());
            //设置简介
            introducetext = (TextView) findViewById(R.id.food_details_food_details_introduce_txt);
            introducetext.setText(results.getResults().get(0).getContent());
            //设置联系电话
            phonetext = (TextView) findViewById(R.id.food_details_food_details_call_number);
            //设置电话字体和颜色
            final String phoneNumber = results.getResults().get(0).getPhone();
            phonetext.setText(phoneNumber);
            phonetext.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
            phonetext.getPaint().setAntiAlias(true);//抗锯齿
            phonetext.setTextColor(getResources().getColor(R.color.blue));
            phonetext.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:" + phoneNumber));
                    startActivity(intent);
                }
            });

            //设置地址
            addresstext = (TextView) findViewById(R.id.food_details_food_details_address_location);
            addresstext.setText(results.getResults().get(0).getAddress());


            listviews = new ArrayList<View>();
            for (int i = 0; i < results.getResults().get(0).getFileList().size(); i++) {
                View itemviewpagerview = LayoutInflater.from(PalyFoodOneDetailsActivity.this).inflate(R.layout.item_food_details_viewpager, null);
                imageView = (ImageView) itemviewpagerview.findViewById(R.id.item_viewpager_paly_food_details_image);
                //获取网络图片并加载到相应的view上面
                GetVolleyHttp.getInstance(PalyFoodOneDetailsActivity.this).
                        setImageLoader(imageView, R.drawable.error_image, R.drawable.error_image, UrlConfig.URL + results.getResults().get(0).getFileList().get(i).getPath());
                //添加到image集合中

                listviews.add(itemviewpagerview);

            }
            //获取viewpager
            viewPager = (ViewPager) findViewById(R.id.food_details_viewpager);
            viewPager.setAdapter(new ViewPagerAdapter(listviews));

            /**
             * 动态的对小圆点进行控制
             */

            LinearLayout mainLayout = (LinearLayout) findViewById(R.id.food_details_head_Relative_spot);
            if (results.getResults().get(0).getFileList().size() != 1&&results.getResults().get(0).getFileList().size() !=0) {

            for (int i = 0; i < results.getResults().get(0).getFileList().size(); i++) {
                LinearLayout linearLayout = new LinearLayout(PalyFoodOneDetailsActivity.this);
                LinearLayout.LayoutParams margin = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                //设置每个小圆点距离左边的间距

                margin.setMargins(10, 0, 0, 0);
                imageView = new ImageView(PalyFoodOneDetailsActivity.this);

                //设置每个小圆点的宽高
                imageView.setLayoutParams(new ViewGroup.LayoutParams(10, 10));
                listimageView.add(imageView);
                if (i == 0) {
                    // 默认选中第一张图片
                    listimageView.get(i)
                            .setBackgroundResource(R.drawable.scenic_tour_round_after_click_click);
                } else {
                    //其他图片都设置未选中状态
                    listimageView.get(i)
                            .setBackgroundResource(R.drawable.scenic_tour_round_after_click_unclick);
                }
                linearLayout.addView(listimageView.get(i), margin);
                mainLayout.addView(linearLayout);
            }
        }
        }
    };


    // 当网络请求返回状态不成功（也就不是200）
    Response.ErrorListener errorlistener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError arg0) {
            Toast.makeText(PalyFoodOneDetailsActivity.this, "网络出了些小问题", Toast.LENGTH_LONG)
                    .show();
        }
    };


}
